import { type CrudConfig } from '@/components';
import urlConstants from '@/constants/urlConstants';
import type { ProColumns } from '@ant-design/pro-components';

// 催收员级别表格数据类型
export interface CollectorLevelItem {
  id?: string;
  factorLevel: string;
  weightCoefficient: number;
}

// 催收员业绩表格数据类型
export interface CollectorPerformanceItem {
  id?: string;
  performanceMinValue: number;
  performanceMaxValue: number;
  weightCoefficient: number;
}

// 权重策略数据类型
export interface WeightSettingRecord {
  paramIndex?: number;
  weightStrategyId: string;
  weightStrategyName: string;
  description?: string;
  collectorLevelWeightedCoefficient?: number;
  collectorPerformanceWeightedCoefficient?: number;
  collectorLevelTable?: CollectorLevelItem[];
  collectorPerformanceTable?: CollectorPerformanceItem[];
  createTime?: string;
  updateTime?: string;
  updateUser?: string;
}

// 统一的页面配置函数 - 接收 translate 函数作为参数
export const getPageConfig = (translate: (prefix: string, key: string) => string) => {
  const t = (key: string) => translate('strategyManage', key);
  const tCommon = (key: string) => translate('common', key);

  return {
    // CRUD配置
    crudConfig: {
      endpoints: {
        list: urlConstants.ADUIT_MANAGE.LIST,
        create: urlConstants.ADUIT_MANAGE.CREATE,
        update: urlConstants.ADUIT_MANAGE.EDIT,
        delete: urlConstants.ADUIT_MANAGE.DELETE,
        detail: urlConstants.ADUIT_MANAGE.LIST,
      },
    } as CrudConfig,

    // 搜索字段配置
    searchFields: [
      {
        name: 'weightStrategyId',
        label: t('weightStrategyId'),
        type: 'input' as const,
      },
      {
        name: 'weightStrategyName',
        label: t('weightStrategyName'),
        type: 'input' as const,
      },
    ],

    // 表格列配置
    columns: [
      {
        title: tCommon('paramIndex'),
        dataIndex: 'paramIndex',
        key: 'paramIndex',
        width: 80,
        fixed: 'left',
      },
      {
        title: t('weightStrategyId'),
        dataIndex: 'weightStrategyId',
        key: 'weightStrategyId',
        width: 150,
      },
      {
        title: t('weightStrategyName'),
        dataIndex: 'weightStrategyName',
        key: 'weightStrategyName',
        width: 150,
      },
      {
        title: t('description'),
        dataIndex: 'description',
        key: 'description',
        width: 200,
        ellipsis: true,
      },
      {
        title: tCommon('updateUser'),
        dataIndex: 'updateUser',
        key: 'updateUser',
        width: 150,
      },
      {
        title: tCommon('updateTime'),
        dataIndex: 'updateTime',
        key: 'updateTime',
        width: 160,
        valueType: 'dateTime',
      },
    ] as ProColumns<WeightSettingRecord>[],

    // 表单分组配置
    formSections: [
      // 基本信息分组
      {
        title: tCommon('baseInfo'),
        fields: [
          {
            name: 'weightStrategyId',
            label: t('weightStrategyId'),
            type: 'input' as const,
            rules: [{ required: true, message: t('weightStrategyIdRequired') }],
          },
          {
            name: 'weightStrategyName',
            label: t('weightStrategyName'),
            type: 'input' as const,
            rules: [{ required: true, message: t('weightStrategyNameRequired') }],
          },
          {
            name: 'description',
            label: t('description'),
            type: 'textarea' as const,
            span: 24,
          },
        ],
      },
      // 催收员级别分组
      {
        title: t('collectorLevel'),
        fields: [
          {
            name: 'collectorLevelWeightedCoefficient',
            label: t('weightedCoefficient'),
            type: 'number' as const,
            rules: [{ required: true, message: t('weightedCoefficientRequired') }],
            span: 12,
            fieldProps: {
              placeholder: tCommon('inputPlaceholder'),
              precision: 2,
              min: 0,
              max: 100,
            },
          },
          {
            name: 'collectorLevelTable',
            label: '', // 隐藏表格的默认标签，因为我们使用section标题
            type: 'editTable' as const,
            span: 24,
            editTableConfig: {
              columns: [
                {
                  title: t('factorLevel'),
                  dataIndex: 'factorLevel',
                  type: 'input' as const,
                  width: 150,
                  required: true,
                },
                {
                  title: t('weightCoefficient'),
                  dataIndex: 'weightCoefficient',
                  type: 'number' as const,
                  width: 150,
                  required: true,
                },
              ],
              rowKey: 'id',
              maxRows: 20,
              minRows: 0,
              addButtonText: t('addCollectorLevel'),
            },
          },
        ],
      },
      // 催收员业绩分组
      {
        title: t('collectorPerformance'),
        fields: [
          {
            name: 'collectorPerformanceWeightedCoefficient',
            label: t('weightedCoefficient'),
            type: 'number' as const,
            rules: [{ required: true, message: t('weightedCoefficientRequired') }],
            span: 12,
            fieldProps: {
              placeholder: tCommon('inputPlaceholder'),
              precision: 2,
              min: 0,
              max: 100,
            },
          },
          {
            name: 'collectorPerformanceTable',
            label: '', // 隐藏表格的默认标签，因为我们使用section标题
            type: 'editTable' as const,
            span: 24,
            editTableConfig: {
              columns: [
                {
                  title: t('performanceMinValue'),
                  dataIndex: 'performanceMinValue',
                  type: 'number' as const,
                  width: 150,
                  required: true,
                },
                {
                  title: t('performanceMaxValue'),
                  dataIndex: 'performanceMaxValue',
                  type: 'number' as const,
                  width: 150,
                  required: true,
                },
                {
                  title: t('weightCoefficient'),
                  dataIndex: 'weightCoefficient',
                  type: 'number' as const,
                  width: 150,
                  required: true,
                },
              ],
              rowKey: 'id',
              maxRows: 20,
              minRows: 0,
              addButtonText: t('addCollectorPerformance'),
            },
          },
        ],
      },
    ],

    // 字典枚举配置
    dictEnum: {},
  };
};
